package com.huiquan.inspection.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.inspection.domain.Index;
import com.huiquan.inspection.domain.IndexDto;
import com.huiquan.inspection.domain.IndexEncyclopedia;
import com.huiquan.inspection.domain.IndexOriginal;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class IndexDao extends EnhancedBaseDao<Index> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "INDEX.";

	public IndexDao() {
		super(space);
	}

	public void updateDisable(Long id, Integer current, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("current", current);
		param.put("userId", user.getUserId());
		param.put("userName", user.getRealName());
		this.update(space + "UPDATE_DISABLE", param, sqlMapClient);
	}

	public int retrieveSizeByName(String name) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		Integer size = (Integer) this.object(space + "RETRIEVE_SIZE_BY_NAME", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	public Index retrieveOjbectByName(String name) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		return (Index)this.object(space + "RETRIEVE_OBJECT_BY_NAME", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> retrieveMapList(Map<String, Object> param) {
		return (List<Map<String, Object>>)this.list(space + "RETRIEVE_MAP_LIST", param, sqlMapClient);
	}

	public void insertEncyclopedia(IndexEncyclopedia indexEncyclopedia) {
		this.insert(space + "INSERT_ENCYCLOPEDIA", indexEncyclopedia, sqlMapClient);
	}

	public IndexEncyclopedia retrieveEncyclopediaByClassIdAndIndexId(long classId, Long indexId) {
		Map<String, Object> param = new HashMap<>();
		param.put("classId", classId);
		param.put("indexId", indexId);
		return (IndexEncyclopedia)this.object(space + "RETRIEVE_ENCYCLOPEDIA_BY_CLASS_ID_AND_INDEX_ID", param, sqlMapClient);
	}

	public void updateEncyclopedia(IndexEncyclopedia ie) {
		this.insert(space + "UPDATE_ENCYCLOPEDIA", ie, sqlMapClient);
	}

	public void updateOriginal(Long id, Long classId, Long indexId, int mutiFlag) {
		Map<String, Object> param = new HashMap<>();
		if(mutiFlag == 1) {
			IndexOriginal io = retrieveOriginal(id);
			param.put("oldClassId", io.getClassId());
			param.put("oldIndexId", io.getIndexId());
		} else {
			param.put("id", id);
		}
		param.put("classId", classId);
		param.put("indexId", indexId);
		param.put("mutiFlag", mutiFlag);
		
		this.update(space + "UPDATE_ORIGINAL", param, sqlMapClient);
	}

	private IndexOriginal retrieveOriginal(Long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		return (IndexOriginal)this.object(space + "RETRIEVE_ORIGINAL", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<IndexDto> retrieveDto() {
		return (List<IndexDto>)this.list(space + "RETRIEVE_DTO", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<IndexDto> retrieveEncyclopediaDto() {
		Map<String, Object> param = new HashMap<>();
		param.put("encyclopediaExist", "true");
		return (List<IndexDto>)this.list(space + "RETRIEVE_DTO", param, sqlMapClient);
	}

	public IndexDto retrieveDtoByOriginalId(String id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		return (IndexDto)this.object(space + "RETRIEVE_DTO", param, sqlMapClient);
	}

	public IndexEncyclopedia retrieveEncyclopediaByClassIdAndNormalName(String classId, String normalName) {
		Map<String, Object> param = new HashMap<>();
		param.put("classId", classId);
		param.put("normalName", normalName);
		return (IndexEncyclopedia)this.object(space + "RETRIEVE_ENCYCLOPEDIA_BY_CLASS_ID_AND_NORMAL_NAME", param, sqlMapClient);
	}

	public void initCategoryInspection() {
		this.insert(space + "INIT_CATEGORY_INSPECTION", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<IndexDto> retrieveCategoryInspection() {
		return (List<IndexDto>)this.list(space + "RETRIEVE_CATEGORY_INSPECTION", sqlMapClient);
	}
	
	public void initSynonymy() {
		Map<String, Object> param = new HashMap<>();
		param.put("regexp", "^[u0391-uFFE5/!@#$%^&*()_+-=,./;\\<>?:{}|`~]*$");
		this.update(space + "INIT_SYNONYMY", param, sqlMapClient);
	}
}
